
class SmsService

  TTL_LIMIT = 60.seconds

  class << self

    def get_valid_sms_code user
      return nil if user.mobile.blank?
      Rails.cache.read(user.mobile)
    end

    def send_msg(options={})
      #options = {project: "项目名称 etV8Q1", vars: {变量名称}}
      flag = false
      begin
        Submail.send("message", options)
        flag = true
      rescue => e
        Rails.logger.error(e)
      end
      flag
    end

    def send_mail(options={})
      options = options.with_indifferent_access
      options = {
        to: options[:email],
        vars: {name: options[:name]},
        links: {link: options[:link]}.to_json
      }
      Submail.send("mail", options, "mail")
    end

    def register_msg to, sms_code
      send_msg({
        project: "PDqpn1",
        to: to,
        vars: {code: sms_code}
      })
    end

    #number: 订单号, 服务商
    def reserve_msg to, number
      send_msg({
        project: "gE6SN2",
        to: to,
        vars: {number: number}
      })
    end

    def reserve_msg_to_admin to, number, cellphone
      send_msg({
        project: "4RfiS2",
        to: to,
        vars: {number: number, cellphone: cellphone}
      })
    end
  
    def auto_register_account to, cellphone, password
      send_msg({
        project: 'wS6sL4',
        to: to,
        vars: {cellphone: cellphone, password: password}
      })
    end

    def tail_payment to, order_no
      send_msg({
        project: 'ZVwh62',
        to: to,
        vars: {number: order_no}
      })
    end

    ##重置密码
    def reset_mail(options={})
      options = options.with_indifferent_access
      options = {
        project: "zDXpH4",
        to: options[:to],
        links: {reset: options[:link]}.to_json
      }
      Submail.send("mail", options)
    end

  end

end
